home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 076-100 / disk_097 / splines / dlist.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  126 lines

  1. /* The routines in this file are copyright (c) 1987 by Helene (Lee) Taran.
  2.  * Permission is granted for use and free distribution as long as the
  3.  * original author's name is included with the code.
  4.  */
  5.  
  6. #include "dlist.h"
  7.  
  8. Init_List(list)
  9. DLISTPTR list; 
  10.  int *calloc();
  11.  list->next = list;
  12.  list->prev = list;
  13.  list->contents = calloc(1,sizeof(int));
  14.  *((int *)(list->contents)) = 0;
  15. }
  16.  
  17.  
  18. /* Inserts <element> before <position> in <list> */
  19. Insert_Before(position, element, list)  
  20. DLISTPTR position, element, list; 
  21.    *((int *)(list->contents)) = LENGTH(list) + 1;
  22.    element->next = position;
  23.    element->prev = position->prev;
  24.    position->prev->next = element;
  25.    position->prev = element;
  26. }
  27.  
  28. /* Inserts <element> after <position> in <list> */
  29. Insert_After(position,element,list)
  30. DLISTPTR position, element, list; 
  31. {
  32.    *((int *)(list->contents)) = LENGTH(list) + 1;
  33.    element->prev = position;
  34.    element->next = position->next;
  35.    position->next->prev = element;
  36.    position->next = element;
  37. }
  38.  
  39. /* Removes <element> from <list>. Assumes the <element> is currently
  40.  * a member of <list>. Does not deallocate <element>
  41.  */
  42. DLISTPTR Remove_Element(element,list)
  43. DLISTPTR element,list;  
  44. {
  45.    if (ISEMPTY(list)) return(list);
  46.    *((int *)(list->contents)) = LENGTH(list) - 1;
  47.    element->prev->next = element->next;
  48.    element->next->prev = element->prev;
  49.    element->next = NULL;
  50.    element->prev = NULL;
  51.    return(element);
  52. }
  53.  
  54.  
  55. /* Member: Returns TRUE if element is a member of the list 
  56.  */
  57. Member (element, list)
  58. DLISTPTR element,list;    
  59.    DLISTPTR tmp = list;
  60.    while ((tmp = tmp->next) != list) 
  61.      if (tmp == element) return(TRUE);     
  62.    return(FALSE);
  63. }
  64.  
  65. /* Get_Member : returns the member of the list that contains <element>
  66.  * returns NULL if not a member of the list.
  67.  */
  68. DLISTPTR GET_MEMBER(element, list) 
  69. void *element; 
  70. DLISTPTR list;   
  71. {
  72.   DLISTPTR tmp = list;
  73.   while ((tmp = tmp->next) != list ) 
  74.      if (tmp->contents == element) return(tmp);     
  75.   return(FALSE);
  76. }
  77.   
  78. /* Find_Element: returns the member of the list that contains some element
  79.  * that satisfies the given test with respect to <element>.  For example:
  80.  * you can define a function equal that returns true if two elements have
  81.  * the exact same fields. 
  82.  * Returns FALSE if no member of the list satifies the test. 
  83.  */
  84. DLISTPTR Find_Element(element,list,test)
  85. void *element;
  86. DLISTPTR list;                 
  87. int (*test)(); 
  88. {
  89.   DLISTPTR tmp = list;
  90.   while ((tmp = tmp->next) != list ) 
  91.      if ((*test)(tmp->contents,element)) return(tmp);     
  92.   return(FALSE);
  93. }
  94.  
  95. /* Apply : applies the given function to the contents of each list
  96.  * element.
  97.  */
  98. Apply(fn,list)
  99. int (*fn)();
  100. DLISTPTR list;
  101. {
  102.    DLISTPTR tmp = list;
  103.    while ((tmp = tmp->next) != list) (*fn)(tmp->contents);
  104.  
  105. }
  106.  
  107.  
  108. /* Removes and disposes of all elements in a list --- does not deallocate
  109.  * the head of the list. if <foo> is non-zero then deallocate the contents
  110.  * of each element.
  111.  */
  112. ClearList (list,foo)
  113. DLISTPTR list; 
  114. int foo;
  115. {
  116.   DLISTPTR doomed;
  117.   while ((doomed = Remove_Element(FIRST(list),list)) != list) {
  118.       if (foo) free(doomed->contents);
  119.       free(doomed);
  120.   }
  121. }
  122.  
  123.